package cn.edu.buu.hospital.web;

import cn.edu.buu.hospital.entity.Doctor;
import cn.edu.buu.hospital.entity.Visit;
import cn.edu.buu.hospital.service.AppointmentService;
import cn.edu.buu.hospital.service.VisitService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 医生工作台控制器
 */
@Controller
@RequestMapping("/doctor")
public class DoctorWorkspaceController {
    
    @Autowired
    private AppointmentService appointmentService;
    
    @Autowired
    private VisitService visitService;
    /**
     * 医生工作台首页
     */
    @GetMapping("/workspace")
    public String workspace(Model model, HttpSession session) {
        // 获取当前登录的医生
        Doctor loginDoctor = (Doctor) session.getAttribute("loginDoctor");
        if (loginDoctor == null) {
            return "redirect:/toDoctorLogin";
        }
        
        // 简化工作台信息
        model.addAttribute("doctor", loginDoctor);
        model.addAttribute("today", java.time.LocalDate.now());
        
        return "backend/doctor/workspace";
    }
    
    /**
     * 查看医生排班信息
     */
    @GetMapping("/appointments")
    public String viewAppointments(Model model, HttpSession session) {
        Doctor loginDoctor = (Doctor) session.getAttribute("loginDoctor");
        if (loginDoctor == null) {
            return "redirect:/toDoctorLogin";
        }
        
        // 查询科室排班信息
        List<Visit> visits = visitService.findByDeptId(loginDoctor.getDeptId());
        model.addAttribute("appointments", visits);
        
        return "backend/doctor/appointments";
    }
    
    /**
     * 查看病人列表
     */
    @GetMapping("/patients")
    public String viewPatients(Model model, HttpSession session) {
        Doctor loginDoctor = (Doctor) session.getAttribute("loginDoctor");
        if (loginDoctor == null) {
            return "redirect:/toDoctorLogin";
        }
        
        // 查询医生的排班信息
        List<Visit> visits = visitService.findByDeptId(loginDoctor.getDeptId());
        model.addAttribute("patientVisits", visits);
        
        return "backend/doctor/patients";
    }
    
    /**
     * 查看病人详情
     */
    @GetMapping("/patient/detail")
    public String viewPatientDetail(@RequestParam("visitId") Long visitId, Model model, HttpSession session) {
        Doctor loginDoctor = (Doctor) session.getAttribute("loginDoctor");
        if (loginDoctor == null) {
            return "redirect:/toDoctorLogin";
        }
        
        // 查询病人就诊记录
        Visit visit = visitService.findById(visitId);
        model.addAttribute("visit", visit);
        
        return "backend/doctor/patientDetail";
    }
    
    /**
     * 写病历单页面
     */
    @GetMapping("/medicalRecord/write")
    public String writeMedicalRecord(@RequestParam("visitId") Long visitId, Model model, HttpSession session) {
        Doctor loginDoctor = (Doctor) session.getAttribute("loginDoctor");
        if (loginDoctor == null) {
            return "redirect:/toDoctorLogin";
        }
        
        // 查询就诊记录
        Visit visit = visitService.findById(visitId);
        model.addAttribute("visit", visit);
        
        return "backend/doctor/writeMedicalRecord";
    }
    

}